package com.easyOrder.core.utils;

import com.easyOrder.core.exception.api.ArgumentsIncorrectException;
import com.easyOrder.core.json.probe.ProbeInfo;

import java.util.Hashtable;

@SuppressWarnings("rawtypes")
public class TypeUtil {
	private static TypeStaticFields staticFields = new TypeStaticFields();

	public static boolean isPrimitive(Class clazz) {
		if ((clazz == boolean.class) || (clazz == Boolean.class)
				|| (clazz == char.class) || (clazz == Character.class)
				|| (clazz == byte.class) || (clazz == Byte.class)
				|| (clazz == short.class) || (clazz == Short.class)
				|| (clazz == int.class) || (clazz == Integer.class)
				|| (clazz == long.class) || (clazz == Long.class)
				|| (clazz == float.class) || (clazz == Float.class)
				|| (clazz == double.class) || (clazz == Double.class)) {
			return true;
		}
		return false;
	}

	public static boolean isStringable(Class clazz) {
		if (isPrimitive(clazz) || (clazz == String.class)
				|| (clazz == Class.class) || clazz.isEnum()
				|| (clazz.isArray() && isStringable(clazz.getComponentType()))) {
			return true;
		}
		return false;
	}

	public static ProbeInfo getCachedProbeInfo(Class clazz) {
		ProbeInfo probeInfo = staticFields.probeInfoFromType.get(clazz);
		if (probeInfo == null) {
			probeInfo = new ProbeInfo(clazz);
			staticFields.probeInfoFromType.put(clazz, probeInfo);
		}
		return probeInfo;
	}

	public static <T> T newInstance(Class<T> clazz) {
		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new ArgumentsIncorrectException(e.getMessage());
		}
	}

	private static class TypeStaticFields {
		Hashtable<Class, ProbeInfo> probeInfoFromType;

		TypeStaticFields() {
			probeInfoFromType = new Hashtable<Class, ProbeInfo>();
		}
	}
}
